#include <my_header.h>

int main(int argc, char *argv[])
{
    char *ip="192.168.222.130";
    char *port="8080";
    int socketfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in sockin;
    struct in_addr in_ip;
    inet_aton(ip,&in_ip);
    sockin.sin_family=AF_INET;
    sockin.sin_addr=in_ip;
    sockin.sin_port=htons(atoi(port));
    int ret_con=connect(socketfd,(struct sockaddr *)&sockin,sizeof(sockin));
    ERROR_CHECK(ret_con,-1,"connect");
    int epollfd=epoll_create(1);
    struct epoll_event event;
    event.events=EPOLLIN;
    event.data.fd=socketfd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,socketfd,&event);
    event.data.fd=STDIN_FILENO;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);
    while(1){
        struct epoll_event list[2];
        int num=epoll_wait(epollfd,list,2,-1);
        char buf[1024];
        memset(buf,0,sizeof(buf));
        for(int i=0;i<num;i++){
            if(list[i].data.fd==socketfd){
                int ret_recv=recv(socketfd,&buf,sizeof(buf),0);
                if(ret_recv==0){
                    printf("服务器断开连接\n");
                    goto end;
                }
                write(STDOUT_FILENO,&buf,sizeof(buf));
            }
            if(list[i].data.fd==STDIN_FILENO){
                int ret_read=read(STDIN_FILENO,&buf,sizeof(buf));
                if(ret_read==0){
                    printf("断开连接\n");
                    goto end;
                }
                send(socketfd,&buf,sizeof(buf),0);
            }

        }
    }
end:
    close(epollfd);
    close(socketfd);
    return 0;
}

